//
//  WSPickerAlterController.m
//  CharonTVProject
//
//  Created by karong on 2023/7/3.
//

#import "WSPickerAlterController.h"
#import "WSFeedbackType.h"

#define CellH 44
@interface WSPickerAlterController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *listView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *listHeight;

@end

@implementation WSPickerAlterController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layoyeView];
    [self layoutHeight];
}

- (void)layoyeView {
    self.titleLabel.text = self.titleStr;
}

- (void)layoutHeight {
    CGFloat height = MIN(self.dataArray.count, 5) * CellH;
    self.listHeight.constant = height;
    [self.listView uploadLayout];
}

- (IBAction)closeAction:(id)sender {
    [self dismiss];
}

#pragma mark UIPickerViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"kcell"];
    UILabel * label = [cell viewWithTag:101];
    id object = self.dataArray[indexPath.row];
    if ([object isKindOfClass:NSClassFromString(@"NSString")]) {
        label.text = (NSString *)object;
    }
    else if ([object isKindOfClass:NSClassFromString(@"WSFeedbackType")]) {
        label.text = ((WSFeedbackType *)object).name;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    id object = self.dataArray[indexPath.row];
    if (self.selectHandle) {
        [self dismissViewControllerAnimated:YES completion:^{
            self.selectHandle(object);
        }];
    }
}

@end
